package com.gzh.animatortext.activity

import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationSet
import android.view.animation.LinearInterpolator
import android.widget.Button
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import com.gzh.animatortext.R
import com.gzh.animatortext.databinding.ActivityCircleBinding
import com.gzh.animatortext.widget.CircleView
import com.gzh.animatortext.widget.RoundedImageView

/**
 * @创建者 顾朝辉
 * @创建时间 2021/5/26 14:49
 * @描述 孤独星球效果
 */
class CircleActivity : AppCompatActivity(), View.OnClickListener {
    lateinit var anim_start: ObjectAnimator
    lateinit var animSet1: AnimatorSet
    lateinit var animSet2: AnimatorSet
    lateinit var animSet3: AnimatorSet
    lateinit var animSet4: AnimatorSet
    lateinit var binding: ActivityCircleBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCircleBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setClick()
        initAnim()
    }

    private fun setClick() {
        binding.buttonPlay.setOnClickListener(this)
    }

    private fun initAnim() {
        //anim_start
        anim_start = ObjectAnimator.ofFloat(binding.ivCover, "rotation", 0f, 360f)
        anim_start.duration = 10000 //旋转一圈10秒
        anim_start.interpolator = LinearInterpolator() //旋转不停顿
        anim_start.repeatCount = ObjectAnimator.INFINITE //一直执行
        //animSet
        animSet1 = AnimatorInflater.loadAnimator(this, R.animator.circle) as AnimatorSet
        animSet1.setTarget(binding.view1)
        animSet2 = AnimatorInflater.loadAnimator(this, R.animator.circle2) as AnimatorSet
        animSet2.setTarget(binding.view2)
        animSet2.startDelay = 2000 //延时2秒开始
        animSet3 = AnimatorInflater.loadAnimator(this, R.animator.circle3) as AnimatorSet
        animSet3.setTarget(binding.view3)
        animSet3.startDelay = 4000
        animSet4 = AnimatorInflater.loadAnimator(this, R.animator.circle4) as AnimatorSet
        animSet4.setTarget(binding.view4)
        animSet4.startDelay = 6000
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.buttonPlay -> {
                if (binding.buttonPlay.text == "播放") {
                    binding.buttonPlay.text = "暂停"
                    if (anim_start.isPaused) {
                        anim_start.resume()
                        animSet1.start()
                        animSet2.start()
                        animSet3.start()
                        animSet4.start()
                    } else {
                        anim_start.start()
                        animSet1.start()
                        animSet2.start()
                        animSet3.start()
                        animSet4.start()
                    }
                } else {
                    binding.buttonPlay.text = "播放"
                    anim_start.pause()
                    animSet1.end()
                    animSet2.end()
                    animSet3.end()
                    animSet4.end()
                }
            }
        }
    }
}